/**
 * Created by hxpeng on 2017/3/24.
 */
(function () {
    'use strict';

    angular.module('App.common')
        .directive('photoPrevire', photoPrevire);

    /**
     * 精彩瞬间选择图片之后预览，
     * @returns {{restrict: string, link: link}}
     */
    function photoPrevire() {
        return {
            restrict:'E',
            replace :true,
            template:'<li><img class="full-image"></li>',
            link: function(scope, elem, attr) {
                scope.$watch('per',function(nowVal){
                    var img = {};
                    var previewStat = false;
                    if(typeof nowVal == 'string'){
                        img.url = nowVal;
                    }else if(typeof nowVal == 'object'){
                        img = nowVal;
                        previewStat = true;
                    }

                    var elem_child = elem[0].childNodes;
                    angular.forEach(elem_child,function(obj){
                        if(obj.nodeName == "IMG"){
                            obj.setAttribute('src',img.url);
                            if(previewStat){
                                obj.onclick=function(){
                                    scope.previewImg(img.url,img.key);
                                }
                            }
                        }
                    })
                })

            }
        };
    }

})();